<template>
  <el-container class="home_container">
    <el-header>
      <el-button type="info" @click="logout">退出</el-button>
      <el-button @click="printThis" type="primary" round>打印this</el-button>
    </el-header>
    <el-container>
      <el-aside :width="menuWidth">
        <button class="open" @click="changeMenuType">| | |</button>
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#3399FF"
          :collapse="isCollapse"
          unique-opened
          :collapse-transition="false"
          router
          :default-active="$router.currentRoute.path"
        >
          <el-submenu
            :index="'/' + item.path"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="menuIcon[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList();
  },
  data() {
    return {
      menuList: [],
      isCollapse: false,
      menuWidth: "200px",
      menuIcon: {
        125: "el-icon-user-solid",
        103: "el-icon-s-cooperation",
        101: "el-icon-s-goods",
        102: "el-icon-s-order",
        145: "el-icon-s-marketing",
      },
    };
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    printThis() {
      console.log(this);
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    changeMenuType() {
      if (this.isCollapse == false) {
        this.isCollapse = true;
        this.menuWidth = "64px";
        return;
      } else {
        this.isCollapse = false;
        this.menuWidth = "200px";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: rgb(54, 61, 64);
}
.el-main {
  background-color: rgb(233, 237, 241);
}
.el-aside {
  background-color: rgb(49, 55, 67);
}
.home_container {
  height: 100%;
}
.open {
  width: 100%;
  background-color: rgb(49, 55, 67);
  border: 4px solid rgb(54, 61, 64);
  color: #fff;
}
.el-menu {
  border-right: 0;
}
</style>